﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

using Fireball.Drawing;

namespace GameImageConvert
{
    public class MyClass
    {
        
        [STAThread]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args")]
        private static void Main(string[] args)
        {
            string dir = @"C:\sandbox\download\asc-2.4.0.0\data\unitsets\mk1";
            foreach(string path in System.IO.Directory.EnumerateDirectories(dir))
            {
                foreach(string file in System.IO.Directory.EnumerateFiles(path))
                {
                    if (file.EndsWith(".pcx"))
                    {
                        GetImage(file);
                    }
                }
            }
        }
        
        public static void GetImage(string name)
        {
            byte[] bytes = (byte[])System.IO.File.ReadAllBytes(name);
            string fileName = Path.GetTempFileName();
            FileInfo fileInfo = new FileInfo(fileName);
            fileInfo.Attributes = FileAttributes.Temporary;
            File.WriteAllBytes(fileName + ".pcx", bytes);
            
            FreeImage ImageSource1 = new FreeImage(fileName + ".pcx");
            File.Delete(fileName + ".pcx");
            
            Bitmap bitmap = ImageSource1.GetBitmap();
            bitmap.MakeTransparent();
            
            string d = Directory.GetCurrentDirectory();
            string clean = Path.GetFileName(name).Replace(".pcx", ".bmp");
            string outPath = Path.Combine(d, clean);
            bitmap.Save(outPath + "");
            Console.WriteLine(outPath);
        }
        
    }
}